// lib/services/push_service.dart
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:aliyun_push/aliyun_push.dart';

// 安装说明
// https://pub.dev/documentation/aliyun_push/1.0.1/
class PushService {
  static bool _isInitialized = false;
  static final AliyunPush _aliyunPush = AliyunPush();

  static Future<void> init() async {
    if (_isInitialized) return;

    try {
      String appKey;
      String appSecret;

      if (Platform.isIOS) {
        appKey = "填写自己iOS项目的appKey";
        appSecret = "填写自己iOS项目的appSecret";
      } else {
        appKey = "填写自己Android项目的appKey";
        appSecret = "填写自己Android项目的appSecret";
      }

      final initResult = await _aliyunPush.initPush(
        appKey: appKey,
        appSecret: appSecret,
      );
      var code = initResult['code'];

      if (code == kAliyunPushSuccessCode) {
        debugPrint('阿里云推送初始化完成');
        _isInitialized = true;

        // 初始化成功后添加回调监听
        _addPushCallback();
      } else {
        String errorMsg = initResult['errorMsg'];
        debugPrint('阿里云推送初始化失败: $errorMsg');
      }
    } catch (e) {
      debugPrint('阿里云推送初始化异常: $e');
    }
  }

  static void _addPushCallback() {
    _aliyunPush.addMessageReceiver(
      onNotification: (Map<dynamic, dynamic> message) {
        debugPrint('收到推送通知: $message');
        return Future.value(); // 添加返回语句
      },
      onNotificationOpened: (Map<dynamic, dynamic> message) {
        debugPrint('用户点击打开通知: $message');
        return Future.value(); // 添加返回语句
      },
      onMessage: (Map<dynamic, dynamic> message) {
        debugPrint('收到自定义消息: $message');
        return Future.value(); // 添加返回语句
      },
      onAndroidNotificationReceivedInApp: (Map<dynamic, dynamic> message) {
        debugPrint('Android应用内通知: $message');
        return Future.value(); // 添加返回语句
      },
      onAndroidNotificationClickedWithNoAction:
          (Map<dynamic, dynamic> message) {
            debugPrint('Android通知点击无跳转: $message');
            return Future.value(); // 添加返回语句
          },
      onIOSRegisterDeviceTokenSuccess: (Map<dynamic, dynamic> message) {
        debugPrint('iOS注册APNs成功: $message');
        return Future.value(); // 添加返回语句
      },
      onIOSRegisterDeviceTokenFailed: (Map<dynamic, dynamic> message) {
        debugPrint('iOS注册APNs失败: $message');
        return Future.value(); // 添加返回语句
      },
    );
  }

  static Future<String?> getDeviceId() async {
    if (!_isInitialized) return null;
    try {
      return await _aliyunPush.getDeviceId();
    } catch (e) {
      debugPrint('获取设备ID失败: $e');
      return null;
    }
  }

  static Future<void> setAlias(String alias) async {
    if (!_isInitialized) return;
    try {
      final result = await _aliyunPush.addAlias(alias);
      var code = result['code'];
      if (code == kAliyunPushSuccessCode) {
        debugPrint('设置别名成功: $alias');
      } else {
        String errorMsg = result['errorMsg'];
        debugPrint('设置别名失败: $errorMsg');
      }
    } catch (e) {
      debugPrint('设置别名异常: $e');
    }
  }

  static Future<void> removeAlias(String alias) async {
    if (!_isInitialized) return;
    try {
      final result = await _aliyunPush.removeAlias(alias);
      var code = result['code'];
      if (code == kAliyunPushSuccessCode) {
        debugPrint('移除别名成功: $alias');
      } else {
        String errorMsg = result['errorMsg'];
        debugPrint('移除别名失败: $errorMsg');
      }
    } catch (e) {
      debugPrint('移除别名异常: $e');
    }
  }

  static Future<void> addTags(List<String> tags) async {
    if (!_isInitialized) return;
    try {
      final result = await _aliyunPush.bindTag(tags);
      var code = result['code'];
      if (code == kAliyunPushSuccessCode) {
        debugPrint('添加标签成功: $tags');
      } else {
        String errorMsg = result['errorMsg'];
        debugPrint('添加标签失败: $errorMsg');
      }
    } catch (e) {
      debugPrint('添加标签异常: $e');
    }
  }

  static Future<void> removeTags(List<String> tags) async {
    if (!_isInitialized) return;
    try {
      final result = await _aliyunPush.unbindTag(tags);
      var code = result['code'];
      if (code == kAliyunPushSuccessCode) {
        debugPrint('移除标签成功: $tags');
      } else {
        String errorMsg = result['errorMsg'];
        debugPrint('移除标签失败: $errorMsg');
      }
    } catch (e) {
      debugPrint('移除标签异常: $e');
    }
  }
}
